using UnityEngine;

public class EnemyController : MonoBehaviour
{
    private Rigidbody2D rigidbody2d;
    public float speed = 3;

    public bool vertical; // 是否是垂直轴的移动
    private int direction = 1; // 方向控制
    private const float changeTime = 2; // 方向改变时间间隔,常量
    private float timer; // 计时器

    private Animator animator;

    // 当前机器人是否故障
    private bool broken;

    // 粒子系统
    public ParticleSystem smokeEffect;
    public GameObject hitEffectParticle; // 被击中的特效

    // 音频播放
    private AudioSource audioSource;
    public AudioClip fixedSound;
    public AudioClip[] hitSounds;

    // Start is called before the first frame update
    void Start()
    {
        rigidbody2d = GetComponent<Rigidbody2D>();
        timer = changeTime;
        animator = GetComponent<Animator>();
        //animator.SetFloat("MoveX", direction);
        //animator.SetBool("Vertical", vertical);
        audioSource = GetComponent<AudioSource>();

        PlayMoveAnimation();
        broken = true;
    }

    // Update is called once per frame
    void Update()
    {
        // 机器人不是损坏状态,则停止移动
        if (!broken)
        {
            return;
        }

        timer -= Time.deltaTime;

        if (timer < 0)
        {
            direction = -direction;
            //animator.SetFloat("MoveX", direction);
            //animator.SetBool("Vertical", vertical);
            PlayMoveAnimation();
            timer = changeTime;
        }

        Vector2 position = rigidbody2d.position;

        if (vertical)
        {
            // 垂直轴向移动
            position.y = position.y + Time.deltaTime * speed * direction;
        }
        else
        {
            // 水平轴向移动
            position.x = position.x + Time.deltaTime * speed * direction;
        }

        rigidbody2d.MovePosition(position);
    }

    // 触发检测
    private void OnCollisionEnter2D(Collision2D collision)
    {
        RubyController rubyController = collision.gameObject.GetComponent<RubyController>();
        if (rubyController != null)
        {
            rubyController.ChangeHealth(-1);
        }
    }

    // 控制移动动画的方法
    private void PlayMoveAnimation()
    {
        if (vertical)
        {
            // 垂直轴向动画的控制
            animator.SetFloat("MoveX", 0);
            animator.SetFloat("MoveY", direction);
        }
        else
        {
            // 水平轴向动画控制
            animator.SetFloat("MoveX", direction);
            animator.SetFloat("MoveY", 0);
        }
    }

    // 修补机器人
    public void Fix()
    {
        // 产生被击中特效
        Instantiate(hitEffectParticle, rigidbody2d.position, Quaternion.identity);

        broken = false;
        rigidbody2d.simulated = false; // 不发生碰撞检测
        animator.SetTrigger("Fixed"); // 进入跳舞状态
        smokeEffect.Stop(); // 停止粒子系统

        audioSource.Stop(); // 停止播放

        // 随机数: 0, 1
        int randomNum = Random.Range(0, 2);
        audioSource.volume = 0.5f; // 设置音量
        audioSource.PlayOneShot(hitSounds[randomNum]);
        Invoke("PlayFixedSound", 0.5f); // 延时调用

        // 完成任务条件
        UIHealthBar.instance.fixedNum++;
    }

    // 播放修复好的音效
    private void PlayFixedSound()
    {
        audioSource.PlayOneShot(fixedSound);
    }

}
